import React, { ReactNode } from 'react'
import Icon from '@/components/Icon'
import styles from './index.module.scss'
import { useHistory } from 'react-router'
import classNames from 'classnames'

type Props = {
  title?: ReactNode
  children?: ReactNode
  right?: ReactNode
  onBack?: () => void
  fixed?: boolean
}

export default function NavBar({ title, right, onBack, children, fixed }: Props) {
  const history = useHistory()
  const back = () => {
    onBack ? onBack() : history.go(-1)
  }
  return (
    <div className={styles.root}>
      <div className={classNames('main', fixed ? 'fixed' : '')}>
        {/* 后退按钮 */}
        <div className="left" onClick={back}>
          <Icon type="iconfanhui" />
        </div>
        {/* 居中标题，插槽优先级更高写前面 */}
        <div className="title">{children || title}</div>
        {/* 右侧内容 */}
        <div className="right">{right}</div>
      </div>
    </div>
  )
}
